package main

import (
	"fmt"
	"go-copyright/eths"

	"go-copyright/configs"
	"go-copyright/routes"

	"github.com/gorilla/sessions"
	"github.com/labstack/echo"
	"github.com/labstack/echo-contrib/session"
	"github.com/labstack/echo/middleware"
)

var EchoObj *echo.Echo //echo框架对象全局定义

//静态html文件的处理
func staticFile() {
	EchoObj.Static("/static", "static") //static全路径

	EchoObj.Static("/assets", "static/pc/assets") //assets路径
	EchoObj.Static("/css", "static/pc/css")       //css路径
	EchoObj.Static("/upload", "static/pc/puload") //upload路径
	EchoObj.Static("/", "static/pc/home")         //home根路径
	EchoObj.Static("/user", "static/pc/user")     //user路径

}

func main() {

	fmt.Printf("get config %v ,%v\n", configs.Config.Common.Port, configs.Config.Db.Connstr)
	EchoObj = echo.New()             //创建echo对象
	EchoObj.Use(middleware.Logger()) //安装日志中间件
	EchoObj.Use(middleware.Recover())
	EchoObj.Use(session.Middleware(sessions.NewCookieStore([]byte("secret")))) //session中间件
	EchoObj.Use(middleware.GzipWithConfig(middleware.GzipConfig{
		Level: 5,
	}))

	go eths.EventSubscribe("ws://localhost:8546", configs.Config.Eth.PxaAddr)

	//设置静态文件处理路径
	staticFile()

	EchoObj.POST("/account", routes.Register)      //用户注册
	EchoObj.POST("/login", routes.Login)           //用户登录
	EchoObj.POST("/content", routes.Upload)        //用户上传图片
	EchoObj.GET("/content", routes.GetPics)        //查看所有用户图片
	EchoObj.GET("/content/:title", routes.GetPic)  //查看单个图片
	EchoObj.POST("/auction", routes.GoAuction)     //发起拍卖
	EchoObj.GET("/auctions", routes.GetAuctions)   //查看拍卖
	EchoObj.GET("/auction/bid", routes.BidAuction) //结束拍卖
	EchoObj.GET("/content/vote", routes.VotePic)   //投票

	EchoObj.GET("/session", routes.GetSession) //session处理

	EchoObj.Logger.Fatal(EchoObj.Start(configs.Config.Common.Port)) //启动服务
}
